<script setup lang="ts">
import { MsgType, PrescriptionStatus } from '@/enum'
import { useUserStore } from '@/stores'
import type { Image } from '@/types/consult'
import type { Message, Prescription } from '@/types/room'
import { showImagePreview, showToast } from 'vant'
import avatar from '@/assets/avatar-doctor.svg'
import dayjs from 'dayjs'
import EvaluateCard from './EvaluateCard.vue'
import { useShowPrescription } from '@/composable'
import { getConsultFlagText, getIllnessTimeText } from '@/utils/filters'
import { useRouter } from 'vue-router'

const store = useUserStore()
const router = useRouter()

defineProps<{
  list: Message[]
}>()

// 2. 预览图片
const showImage = (pictures?: Image[]) => {
  if (Array.isArray(pictures) && pictures.length > 0) {
    showImagePreview(pictures.map((item) => item.url))
  }
}

// 3. 格式化时间
const formatTime = (time: string) => {
  return dayjs(time).format('hh:mm')
}

// 4. 监听图片加载完毕
const loadedImage = (scroll?: boolean) => {
  if (scroll) {
    console.log('图片加载完毕')
    // 让页面滚动到最底部
    window.scrollTo(0, document.body.scrollHeight)
  }
}

// 5. 展示原始处方单
const { showPrePic } = useShowPrescription()

// 6. 去购买药品
/*
如果处方失效：提示即可
如果处方的药品没付款且有订单ID，代表已经生成药品订单没付款：去药品订单详情付款
如果没付款且没药品订单ID：去药品预支付页面
*/
const buy = (pre: Prescription) => {
  // 6.1 如果处方失效：提示即可
  if (pre.status === PrescriptionStatus.Invalid)
    return showToast('当前处方已经失效')
  // 6.2 如果处方的药品没付款且有药品订单ID，代表已经生成药品订单没付款：去药品订单详情付款
  // (注意: 传药品订单id)
  if (pre.status === PrescriptionStatus.NotPayment && pre.orderId) {
    return router.push(`/order/${pre.orderId}`)
  }
  // 6.3 如果没付款且没药品订单ID：去药品预支付页面
  // (注意: 传处方的id)
  router.push(`/order/pay?id=${pre.id}`)
}
</script>

<template>
  <template
    v-for="{ id, msgType, msg, from, createTime, fromAvatar, scroll } in list"
    :key="id"
  >
    <!-- 患者卡片 -->
    <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
      <div class="patient van-hairline--bottom">
        <p>
          {{ msg.consultRecord?.patientInfo.name }}
          {{ msg.consultRecord?.patientInfo.genderValue }}
          {{ msg.consultRecord?.patientInfo.age }}岁
        </p>
        <p>
          {{ getIllnessTimeText(msg.consultRecord?.illnessTime) }} |
          {{ getConsultFlagText(msg.consultRecord?.consultFlag) }}
        </p>
      </div>
      <van-row>
        <van-col span="6">病情描述</van-col>
        <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
        <van-col span="6">图片</van-col>
        <van-col span="18" @click="showImage(msg.consultRecord?.pictures)"
          >点击查看</van-col
        >
      </van-row>
    </div>
    <!-- 通知-通用 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.Notify">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 通知-温馨提示 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
      <div class="content">
        <span class="green">温馨提示：</span>
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 通知-结束 -->
    <div
      class="msg msg-tip msg-tip-cancel"
      v-if="msgType === MsgType.NotifyCancel"
    >
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 发送文字 -->
    <div
      class="msg msg-to"
      v-if="msgType === MsgType.MsgText && from === store.user?.id"
    >
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image :src="fromAvatar" />
    </div>
    <!-- 发送图片 -->
    <div
      class="msg msg-to"
      v-if="msgType === MsgType.MsgImage && from === store.user?.id"
    >
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image
          fit="contain"
          :src="msg.picture?.url"
          @click="showImage([msg.picture as Image])"
          @load="loadedImage(scroll)"
        />
      </div>
      <van-image :src="fromAvatar" />
    </div>
    <!-- 接收文字 -->
    <div
      class="msg msg-from"
      v-if="msgType === MsgType.MsgText && from !== store.user?.id"
    >
      <van-image :src="avatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>
    <!-- 接收图片 -->
    <div
      class="msg msg-from"
      v-if="msgType === MsgType.MsgImage && from !== store.user?.id"
    >
      <van-image :src="avatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image
          fit="contain"
          :src="msg.picture?.url"
          @click="showImage([msg.picture as Image])"
          @load="loadedImage(scroll)"
        />
      </div>
    </div>
    <!-- 处方卡片 -->
    <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre">
      <div class="content">
        <div class="head van-hairline--bottom">
          <div class="head-tit" @click="showPrePic(msg.prescription?.id)">
            <h3>电子处方</h3>
            <p>原始处方 <van-icon name="arrow"></van-icon></p>
          </div>
          <p>
            {{ msg.prescription?.name }}
            {{ msg.prescription?.genderValue }} {{ msg.prescription?.age }}岁
            {{ msg.prescription?.diagnosis }}
          </p>
          <p>开方时间: {{ msg.prescription?.createTime }}</p>
        </div>
        <div class="body">
          <div
            class="body-item"
            v-for="med in msg.prescription?.medicines"
            :key="med.id"
          >
            <div class="durg">
              <p>{{ med.name }} {{ med.specs }}</p>
              <p>{{ med.usageDosag }}</p>
            </div>
            <div class="num">x{{ med.quantity }}</div>
          </div>
        </div>
        <div class="foot">
          <span @click="buy(msg.prescription!)">购买药品</span>
        </div>
      </div>
    </div>
    <!-- 评价卡片，后期实现 -->
    <div
      class="msg"
      v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm"
    >
      <evaluate-card :evaluateDoc="msg.evaluateDoc"></evaluate-card>
    </div>
  </template>
</template>

<style lang="scss" scoped>
@import '@/styles/room.scss';
</style>
